package com.example.evaluate.service;

import com.example.evaluate.mapper.StudentMapper;
import com.example.evaluate.mapper.StudentTeacherMapper;
import com.example.evaluate.mapper.TeacherCourseMapper;
import com.example.evaluate.mapper.TeacherMapper;
import com.example.evaluate.model.Student;
import com.example.evaluate.model.TeacherCourse;
import com.example.evaluate.model.TrueTeacher;
import com.example.evaluate.model.temp.StudentInfo;
import com.example.evaluate.model.temp.TeacherInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TempService implements ITempService{

    @Autowired
    StudentMapper studentMapper;

    @Autowired
    TeacherCourseMapper teacherCourseMapper;

    @Autowired
    StudentTeacherMapper studentTeacherMapper;

    @Autowired
    TeacherMapper teacherMapper;

    /**
     * 获取所有的tempStudent
     */
    @Override
    public List<StudentInfo> getAllStudentInfo() {
        return getStudentInfo(studentMapper.findAllStudent());
    }

    /**
     * 根据班级获取tempStudent
     *
     * @param studentClass
     */
    @Override
    public List<StudentInfo> getStudentInfoByClass(String studentClass) {
        return getStudentInfo(studentMapper.findStudentByStudentClass(studentClass));
    }

    /**
     * 根据班级获取student信息
     *
     * @param studentClass
     */
    @Override
    public List<Student> getStudentByClass(String studentClass) {
        return studentMapper.findStudentByStudentClass(studentClass);
    }

    /**
     * 获取所有的TeacherInfo信息
     * 用于数据的渲染
     */
    @Override
    public List<TeacherInfo> getAllTeacherInfo() {
        //先找到TrueTeacher列表
        List<TrueTeacher> trueTeachers = teacherMapper.findAllTrueTeacher();
        List<TeacherInfo> teacherInfos = new ArrayList<>();

        //遍历 找course
        for (TrueTeacher tt:
                trueTeachers) {
            List<TeacherCourse> teacherCourses = teacherCourseMapper.findCourseByTrueTeacherId(tt.getTrueTeacherId());
            for (TeacherCourse tc:teacherCourses
                 ) {
                int a = teacherMapper.getTotalStudentNumberByCourseName(tc.getCourseName());
                int b = teacherMapper.getEvaluatedNumberByCourseName(tc.getCourseName());
                tc.setEvaluate(b,a);
                tc.setNumberOfEvaluateStudent(b);
                tc.setNumberOfStudent(a);
            }

            TeacherInfo t = new TeacherInfo(tt,teacherCourses);
            teacherInfos.add(t);
        }

        return teacherInfos;
    }

    /**
     * 通过TrueTeacherId找到TeacherInfo
     *
     * @param trueTeacherId
     */
    @Override
    public TeacherInfo getTeacherInfoByTrueTeacherId(int trueTeacherId) {
        TeacherInfo teacherInfo = new TeacherInfo();
        TrueTeacher t = teacherMapper.findTrueTeacherByTeacherId(trueTeacherId);
        List<TeacherCourse> tcs = teacherCourseMapper.findCourseByTrueTeacherId(trueTeacherId);
        for (TeacherCourse tc:tcs
        ) {
            int a = teacherMapper.getTotalStudentNumberByCourseName(tc.getCourseName());
            int b = teacherMapper.getEvaluatedNumberByCourseName(tc.getCourseName());
            tc.setNumberOfEvaluateStudent(b);
            tc.setNumberOfStudent(a);
        }

        teacherInfo.setTeacherCourses(tcs);
        teacherInfo.setTrueTeacher(t);


        return teacherInfo;
    }

    /**
     * 封装的公共方法
     * */
    private List<StudentInfo> getStudentInfo(List<Student> students){
        List<StudentInfo> studentInfos = new ArrayList<>();
        for (Student s:
                students) {
            StudentInfo studentInfo = new StudentInfo(s.getStudentId(),s.getStudentName()
                    ,s.getStudentAcademy(),s.getStudentMajor(),s.getStudentClass()
                    ,studentTeacherMapper.findAllTeacherByStudentId(s.getStudentId()));
            studentInfos.add(studentInfo);
        }
        return studentInfos;
    }
}
